

********* This program generates a dataset that combines i, j, and k links in one *********
* This is part 2 of the program. Run this after running part 1 and the code in R


clear all
set more off, permanently
cd "$localdir\Data"
global output "$localdir\Output"


*** i-k dataset (RF) after all restrictions

use "Unemployment Outcomes RF for Analysis - Last Year.dta", clear
rename unique_network unique_k
keep year unique unique_k
save "i-k 2010-2013 final sample", replace

*** j-k data set

use "J of Survey Members", clear
g V1 = _n
save "J of Survey Members code.dta", replace

use "network_pairs_jk 2010", clear
g year = 2010

append using "network_pairs_jk 2011"
replace year = 2011 if year==.

append using "network_pairs_jk 2012"
replace year = 2012 if year==.

append using "network_pairs_jk 2013"
replace year = 2013 if year==.

* J members
merge m:1 V1 using "J of Survey Members code.dta"
keep if _merge==3
drop V1 _merge
rename unique unique_j
rename V2 unique_k

* Keep year, unique_j, unique_k
keep year unique_j unique_k
sort unique_j
save "j-k 2010-2013", replace // For use later in the code

*** Join FS dataset (i-j) and j-k dataset 

forval i=2010/2013 {
use "FS 2010-2013", clear
keep if year==`i'
save "FS `i'", replace
}

forval i=2010/2013 {
use "j-k 2010-2013", clear
keep if year==`i'
save "j-k `i'", replace
}

* Join each year separately

forval i=2010/2013 {
use "FS `i'", clear
joinby unique_j using "j-k `i'", unm(both)
tab _merge
keep if _merge==3
drop _merge
save "i-j-k `i'", replace 
}


* Merge with final i-k sample

forval i=2010/2013 {
use "i-k 2010-2013 final sample", clear
keep if year==`i'
save "i-k `i' final sample", replace
}

forval i=2010/2013 {
use "i-j-k `i'", clear // year-specific j-links from the FS with year-specific k-links, for all unique survey-i's
merge m:1 unique unique_k using "i-k `i' final sample" 
* Only in master data if it is an i who is not in the survey in that specific year, or an i-k subject to restrictions
* Only in using data: should not be possible
* Only use the matched ones
keep if _merge==3
drop _merge
save "i-j-k `i' final sample", replace
}

* Collect all i-j-k datasets
use "i-j-k 2010 final sample", clear
drop _merge

append using "i-j-k 2011 final sample"
drop _merge

append using "i-j-k 2012 final sample"
drop _merge

append using "i-j-k 2013 final sample"
drop _merge

save "i-j-k 2010-2013 final sample", replace




************ Identify type of network link *************

*** The i-j link

forval i=2010/2013 {

use "partner_pairs `i'", clear
rename V1 unique
rename V2 unique_j
save "i-j partner `i'", replace

* Family
use "family_pairs `i'", clear
rename V1 unique
rename V2 unique_j
save "i-j family `i'", replace

* Education
use "educ_pairs `i'", clear
rename V1 unique
rename V2 unique_j
save "i-j educ `i'", replace

* Work
use "coworker_pairs `i'", clear
rename V1 unique
rename V2 unique_j
save "i-j coworker `i'", replace

* Identify links
use "i-j-k `i' final sample", clear
drop _merge

merge m:1 unique unique_j using "i-j partner `i'"
keep if _merge==1 | _merge==3
g partner=(_merge==3)
drop _merge

merge m:1 unique unique_j using "i-j family `i'"
keep if _merge==1 | _merge==3
g family=(_merge==3)
drop _merge

merge m:1 unique unique_j using "i-j educ `i'"
keep if _merge==1 | _merge==3
g educ=(_merge==3)
drop _merge

merge m:1 unique unique_j using "i-j coworker `i'"
keep if _merge==1 | _merge==3
g coworker=(_merge==3)
drop _merge

egen links=rowtotal( partner family educ coworker )
tab links

save "i-j-k with j-type `i'", replace

}


* The j-k link

forval i=2010/2013 {

use "partner_pairs `i'", clear
rename V1 unique_j
rename V2 unique_k
save "j-k partner `i'", replace

* Family
use "family_pairs `i'", clear
rename V1 unique_j
rename V2 unique_k
save "j-k family `i'", replace

* Education
use "educ_pairs `i'", clear
rename V1 unique_j
rename V2 unique_k
save "j-k educ `i'", replace

* Work
use "coworker_pairs `i'", clear
rename V1 unique_j
rename V2 unique_k
save "j-k coworker `i'", replace

* Identify links
use "i-j-k with j-type `i'", clear

merge m:1 unique_j unique_k using "j-k partner `i'"
keep if _merge==1 | _merge==3
g partner_j=(_merge==3)
drop _merge

merge m:1 unique_j unique_k using "j-k family `i'"
keep if _merge==1 | _merge==3
g family_j=(_merge==3)
drop _merge

merge m:1 unique_j unique_k using "j-k educ `i'"
keep if _merge==1 | _merge==3
g educ_j=(_merge==3)
drop _merge

merge m:1 unique_j unique_k using "j-k coworker `i'"
keep if _merge==1 | _merge==3
g coworker_j=(_merge==3)
drop _merge

egen links_j=rowtotal( partner_j family_j educ_j coworker_j )
tab links_j

save "i-j-k with j-type and k-type `i'", replace

}

* Collect all years
use "i-j-k with j-type and k-type 2010", clear
append using "i-j-k with j-type and k-type 2011"
append using "i-j-k with j-type and k-type 2012"
append using "i-j-k with j-type and k-type 2013"
save "i-j-k with j-type and k-type 2010-2013", replace

use "i-j-k with j-type and k-type 2010-2013", clear 
*** Number of j's linking k to i
bysort year unique unique_k: gen count=_n
bysort year unique unique_k: egen j_count=max(count)
tab j_count
drop count

*** Frequency of discussion i-j 

merge m:1 unique using "Frequency of discussion 2015"
drop if _merge==2
drop _merge

* Keep frequency of discussion about unemployment and politics
drop freq_imm_parents-freq_fin_educ freq_imm_family freq_fin_family 

* Take the discussion freq relevant to the link, take average if the link is of more types (e.g. both family and colleauge)
replace freq_unemp_work=. if coworker==0
replace freq_pol_work=. if coworker==0
replace freq_unemp_educ=. if educ==0
replace freq_pol_educ=. if educ==0
replace freq_unemp_family=. if (partner==0 & family==0)
replace freq_pol_family=. if (partner==0 & family==0)

egen freq_unemp_link=rowmean(freq_unemp_work freq_unemp_educ freq_unemp_family)
egen freq_pol_link=rowmean(freq_pol_work freq_pol_educ freq_pol_family)

save "i-j-k links for RF", replace




